<?php
$servername = 'localhost';
$username = 'username';
$password = 'password';
$dbname = 'myDB';

// 创建连接
$connect = mysqli_connect($servername, $username, $password);
 
// 检测连接
if (!$connect) {
    die("Connection failed: " . mysqli_connect_error());
} 
echo "连接成功" . "<br />";

// 选择数据库
mysqli_select_db($connect, $dbname);
// 设置编码，防止中文乱码
mysqli_query($connect, "set names utf8");

// 查询数据并输出
$sql = 'SELECT id, title, author, _date FROM myTable ORDER BY title DESC';
$retval = mysqli_query($connect, $sql);
if (!$retval) {
    die('ERROR: ' . mysqli_error($connect));
} else {
    echo '<table border="1"><tr><td>ID</td><td>标题</td><td>主角</td><td>时间</td></tr>';
    // 使用 mysqli_fetch_assoc 获取数据
    while ($row = mysqli_fetch_assoc ($retval)) {
        echo "<tr><td>{$row['id']}</td>" .
             "<td>{$row['title']}</td>" . 
             "<td>{$row['author']}</td>" . 
             "<td>{$row['_date']}</td></tr>";
    }
    // 使用 mysqli_fetch
    echo '</table>' . '<br />';
    // 释放内存
    mysqli_free_result($retval);
}

// 关闭数据库连接
mysqli_close($connect);